import { config, request } from 'utils';
import * as commonService from "utils/services";

const { api } = config;
const { orderList, contractOrders, financialAccountBalance } = api;

export async function query(params = {}) {
  params.apiBaseUrl = `${orderList}`;
  return commonService.commonQuery(params);
}
export async function queryContracts(params = {}) {
  params.apiBaseUrl = `${contractOrders}`;
  return commonService.commonQuery(params);
}
export async function countContracts(params = {}) {
  const url = `${contractOrders}/count`;
  return request({ url: url, method: 'GET', data: params })
}


/**
 * 订单详情
 * @param {*} id
 */
export async function detail(id) {
  const url = `${orderList}/${id}/detail`;
  return request({ url: url, method: 'GET', data: {} })
}

/**
 * 创建订单
 * @param {*} id
 * @param {*} version
 */
export async function createOrder(params) {
  const url = `${orderList}/create`;
  return request({ url: url, method: 'POST', data: params })
}

/**
 * 修改订单
 * @param {*} id
 * @param {*} params
 */
export async function updateOrder(id, params) {
  const url = `${orderList}/${id}/update`;
  return request({ url: url, method: 'PUT', data: params })
}

/**
 * 删除订单
 * @param {*} id
 * @param {*} params
 */
export async function deleteOrder(id) {
  const url = `${orderList}/${id}/delete`;
  return request({ url: url, method: 'GET', data: {} })
}

/**
 * 订单下单
 * @param {*} id
 * @param {*} version
 */
export async function submitOrder(id, version) {
  const url = `${orderList}/${id}/submit/${version}`;
  return request({ url: url, method: 'PUT', data: {} })
}

/**
 * 新增合同
 * @param {*} orderId
 * @param {*} submitData
 */
export async function addContractOrder(id, contractOrder) {
  const url = `${orderList}/${id}/contractOrder/create`;
  return request({ url: url, method: 'POST', data: contractOrder })
}

/**
 * 删除合同
 * @param {*} id
 * @param {*} contractOrderId
 */
export async function deleteContractOrder(id, contractOrderId) {
  const url = `${orderList}/${id}/contractOrder/${contractOrderId}/delete`;
  return request({ url: url, method: 'DELETE' })
}

/**
 * 合同方案明细
 * @param {*} id
 * @param {*} contractOrderId
 */
export async function contractOrderDetail(id, contractOrderId) {
  const url = `${orderList}/${id}/contractOrder/${contractOrderId}/detail`;
  return request({ url: url, method: 'GET', data: {} })
}

/**
 * 修改合同
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} contractOrder
 */
export async function updateContractOrder(id, contractOrderId, contractOrder) {
  delete contractOrder.id;
  const url = `${orderList}/${id}/contractOrder/${contractOrderId}/update`;
  return request({ url: url, method: 'PUT', data: contractOrder })
}

/**
 * 提交设计方案
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} params
 */
export async function doDesign(id, contractOrderId, params) {
  const url = `${orderList}/${id}/contractOrder/${contractOrderId}/doDesign`;
  return request({ url: url, method: 'POST', data: params })
}

/**
 * 提交合同
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 */
export async function placeOrder(id, contractOrderId, version) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/placeOrder/${version}`;
  return request({ url: url, method: 'POST' })
}

/**
 * 设计方案通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 */
export async function acceptDesign(id, contractOrderId, version) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/acceptDesign/${version}`;
  return request({ url: url, method: 'POST' })
}

/**
 * 设计方案不通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 * @param {*} params
 */
export async function denyDesign(id, contractOrderId, version, params) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/denyDesign/${version}`;
  return request({ url: url, method: 'POST', data: params })
}

/**
 * 订单报价
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} quoteParams
 */
export async function quoteOrder(id, contractOrderId, quoteParams) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/quoteOrder`;
  return request({ url: url, method: 'POST', data: quoteParams })
}

/**
 * 报价方案通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 */
export async function acceptQuote(id, contractOrderId, version) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/acceptQuote/${version}`;
  return request({ url: url, method: 'POST' })
}

/**
 * 报价方案不通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 * @param {*} params
 */
export async function denyQuote(id, contractOrderId, version, params) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/denyQuote/${version}`;
  return request({ url: url, method: 'POST', data: params })
}

/**
 * 推送订单到OMS
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} publishParams
 */
export async function publishOrder(id, contractOrderId, publishParams) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/publishOrder`;
  return request({ url: url, method: 'POST', data: publishParams })
}

/**
 * 再次推送订单到OMS
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} publishParams
 */
export async function publishAgain(id, contractOrderId, publishParams) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/publishAgain`;
  return request({ url: url, method: 'POST', data: publishParams })
}

/**
 * OMS报价通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 */
export async function acceptOmsQuote(id, contractOrderId, version) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/acceptOmsQuote/${version}`;
  return request({ url: url, method: 'POST', data: {} })
}

/**
 * OMS报价不通过
 * @param {*} id
 * @param {*} contractOrderId
 * @param {*} version
 */
export async function denyOmsQuote(id, contractOrderId, version) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/denyOmsQuote/${version}`;
  return request({ url: url, method: 'POST', data: {} })
}

export async function isPushOms(id, contractOrderId, params) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/isPushOms`;
  return request({ url: url, method: 'POST', data: params })
}

export async function toTenant(id, contractOrderId, params) {
  const url = `${orderList}/${id}/contractOrders/${contractOrderId}/toTenant`;
  return request({ url: url, method: 'POST', data: params })
}
export async function getTenantOverall(id, contractOrderId, params) {
  const url = `${financialAccountBalance}/tenantOverall`;
  return request({ url: url, method: 'GET' })
}

export async function refreshFromOms(contractNum) {
  const url = `${contractOrders}/refresh/${contractNum}`;
  return request({ url: url, method: 'GET' })
}

export async function queryLogisticCompany(contractNum) {
  const url = `${contractOrders}/logisticCompany/list`;
  return request({ url: url, method: 'GET' })
}

export async function editOuterLogistics({contractNum, ...params}) {
  const url = `${contractOrders}/logisticInfo/update/${contractNum}`;
  return request({ url: url, method: 'POST', data: params })
}